package com.bsoft.gol.hcs.visitrecord.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import com.bsoft.gol.hcs.enumcase.FollowTypeEnum;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.specialist.bean.response.FactorData;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.visitrecord.entity.VrVisitDiagnosis;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ChenJij on 2020/9/4.
 */
public abstract class VrVisitDiagnosisDAO extends HibernateSupportDelegateDAO<VrVisitDiagnosis> {
    public VrVisitDiagnosisDAO() {
        this.setKeyField("diagnosticRecordId");
    }

    /**
     * 更新删除标识
     */
    @DAOMethod(sql = "update VrVisitDiagnosis set deleteFlag ='1' where  businessId in(:businessIdList) ")
    public abstract void updateDeleteFlag(@DAOParam("businessIdList") List<String> businessIdList);

    /**
     * 查询入院诊断
     */
    @DAOMethod(sql = " from VrVisitDiagnosis where businessId=:businessId and diagnosisServiceType='1' ")
    public abstract List<VrVisitDiagnosis> queryVrVisitDiagnosisByBusinessId(@DAOParam("businessId") String businessId);

    /**
     * 根据businessId查询诊断数据
     */
    @DAOMethod(sql = " from VrVisitDiagnosis where businessId=:businessId ")
    public abstract List<VrVisitDiagnosis> queryVrVisitDiagnosis(@DAOParam("businessId") String businessId);


    /**
     * @description: 根据诊断编码、诊断时间、病人id集合查询诊断记录集合
     * @param: factorIdList 诊断编码集合
     * @param: diagnosisTime
     * @param: diagnosisServiceType 业务诊断类型：1入院诊断、2出院诊断、3门诊诊断、5其他诊断
     * @param: patientIdList
     * @return: java.util.List<specialist.bean.response.FactorData>
     */
    public List<FactorData> executeFactorForDiagnosis(List<String> factorIdList, Date startTime, Date endTime, String diagnosisServiceType, List<String> patientIdList, Byte isLoopExecute, Long routeDetailId, String followType) {
        HibernateStatelessResultAction<List<FactorData>> action = new AbstractHibernateStatelessResultAction<List<FactorData>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("select patientId,diagnosticRecordId,diagnosisServiceType,diseasesCode ,diseasesName,diagnosisTime,dataSource from (");
                sqlBuilder.append("SELECT patient_id as patientId,diagnostic_record_id diagnosticRecordId,diagnosis_service_type diagnosisServiceType, ");
                sqlBuilder.append(" diseases_code diseasesCode,diseases_name diseasesName,diagnosis_time diagnosisTime,'1' dataSource");
                sqlBuilder.append("  FROM `vr_visit_diagnosis`  ");
                sqlBuilder.append(" WHERE diseases_code in(:diseasesCodeList) and diagnosis_service_type=:diagnosisServiceType ");
                if (null != startTime) {
                    sqlBuilder.append("  and  diagnosis_time>:startTime ");
                }
                if (null != endTime) {
                    sqlBuilder.append("  and  diagnosis_time<:endTime ");
                }
                if (CommonUtils.isNotEmpty(patientIdList)) {
                    sqlBuilder.append(" and patient_id in(:patientIdList)");
                }
                sqlBuilder.append(") AS  m ");
                //循环执行机制 0 不执行 1 执行
                if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
                    sqlBuilder.append(" LEFT JOIN fus_factor_data_record f  ON f.patient_id = m.patientId   AND f.route_detail_id = :routeDetailId AND f.diagnostic_record_id = m.diagnosticRecordId WHERE f.id IS NULL  ");
                }
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                query.setParameterList("diseasesCodeList", factorIdList);
                query.setParameter("diagnosisServiceType", diagnosisServiceType);
                if (null != startTime) {
                    query.setParameter("startTime", startTime);
                }
                if (null != endTime) {
                    query.setParameter("endTime", endTime);
                }
                if (CommonUtils.isNotEmpty(patientIdList)) {
                    query.setParameterList("patientIdList", patientIdList);
                }
                if (FollowTypeEnum.FUS_SPECIALIST.getType().equals(followType) && null != isLoopExecute && isLoopExecute == 1) {
                    query.setParameter("routeDetailId", routeDetailId);
                }
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                List<FactorData> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, FactorData.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}